package com.example.ai.inventory.controller;

import com.example.ai.inventory.entity.SalesOrder;
import com.example.ai.inventory.entity.SalesItem;
import com.example.ai.inventory.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 销售控制器
 */
@RestController
@RequestMapping("/api/sales")
public class SalesController {
    
    @Autowired
    private SalesService salesService;
    
    /**
     * 创建销售单
     */
    @PostMapping
    public SalesOrder createSalesOrder(@RequestBody SalesOrder order) {
        return salesService.createSalesOrder(order);
    }
    
    /**
     * 根据ID获取销售单
     */
    @GetMapping("/{id}")
    public SalesOrder getSalesOrderById(@PathVariable Long id) {
        return salesService.getSalesOrderById(id);
    }
    
    /**
     * 根据销售单号获取销售单
     */
    @GetMapping("/orderNo/{orderNo}")
    public SalesOrder getSalesOrderByOrderNo(@PathVariable String orderNo) {
        return salesService.getSalesOrderByOrderNo(orderNo);
    }
    
    /**
     * 获取销售单列表
     */
    @GetMapping
    public List<SalesOrder> getSalesOrders(
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        return salesService.getSalesOrders(startTime, endTime);
    }
    
    /**
     * 获取销售明细
     */
    @GetMapping("/{id}/items")
    public List<SalesItem> getSalesItemsByOrderId(@PathVariable Long id) {
        return salesService.getSalesItemsByOrderId(id);
    }
    
    /**
     * 获取商品的销售历史
     */
    @GetMapping("/product/{productId}")
    public List<SalesItem> getSalesHistoryByProductId(@PathVariable Long productId) {
        return salesService.getSalesHistoryByProductId(productId);
    }
}